package com.sc.web.gwt.server.persistence;

import com.sc.web.gwt.shared.model.Admin;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class AdminPersistence extends AbstractDBConnection {
    public List<Admin> getAllAdmins() {
        List<Admin> adminList = new ArrayList<Admin>();
        String query = "SELECT AdminId, AdminAccount, DES_DECRYPT(AdminPassword) FROM ADMINS";
        try {
            Connection connection = getConnection();
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery(query);
            while (resultSet.next()) {
                adminList.add(new Admin(resultSet.getInt(1), resultSet.getString(2), resultSet.getString(3)));
            }

            resultSet.close();
            connection.close();

        } catch (Exception e) {
            System.err.println("Mysql Statement Error: " + query);
            e.printStackTrace();
        }

        return adminList;
    }

    public Admin getAdmin(int adminId) {
        Admin admin = new Admin();
        String query = "SELECT AdminId, AdminAccount, DES_DECRYPT(AdminPassword) FROM ADMINS WHERE ADMINID = ?";
        try {
            Connection connection = getConnection();
            PreparedStatement ps = connection.prepareStatement(query);
            ps.setInt(1, adminId);
            ResultSet resultSet = ps.executeQuery();
            if (resultSet.first()) {
                admin = new Admin(resultSet.getInt(1), resultSet.getString(2), resultSet.getString(3));
            }

            resultSet.close();
            connection.close();

            return admin;
        } catch (Exception e) {
            System.err.println("Mysql Statement Error: " + query);
            e.printStackTrace();
            return null;
        }


    }

    public boolean updateAdmin(Admin admin) {
        String query = "UPDATE admins\n" +
                "SET AdminAccount = ?, AdminPassword = DES_ENCRYPT(?) \n" +
                "WHERE AdminId = ?";
        try {
            Connection connection = getConnection();
            PreparedStatement ps = connection.prepareStatement(query);
            ps.setString(1, admin.getAdminAccount());
            ps.setString(2, admin.getAdminPassword());
            ps.setInt(3, admin.getAdminId());

            ps.execute();
            connection.close();
        } catch (Exception e) {
            System.err.println("Mysql Statement Error: " + query);
            e.printStackTrace();
            return false;
        }

        return true;
    }

    public boolean insertAdmin(Admin admin) {
        String query = "INSERT INTO admins (adminAccount,adminPassword)\n" +
                "VALUES (?,DES_ENCRYPT(?))";
        try {
            Connection connection = getConnection();
            PreparedStatement ps = connection.prepareStatement(query);
            ps.setString(1, admin.getAdminAccount());
            ps.setString(2, admin.getAdminPassword());

            ps.execute();
            connection.close();
        } catch (Exception e) {
            System.err.println("Mysql Statement Error: " + query);
            e.printStackTrace();
            return false;
        }

        return true;
    }
}
